package com.ego.controller;

import com.ego.dao.OrderDao;
import com.ego.dao.impl.OrderDaoImpl;
import com.ego.pojo.Order;
import com.ego.vo.OrderItemVo;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

/**
 * @author 袁梦达 2019012364
 */
@WebServlet("/OrderDetailShowServlet")
public class OrderDetailShowServlet extends HttpServlet {

    private OrderDao orderDao = new OrderDaoImpl();

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");

        String userNumber = String.valueOf(getServletContext().getAttribute("userNumber"));
        if(userNumber == null){
            //未登录
            response.getWriter().print("");
        }else {

            //获取详细展示的商品的ID
            InputStream is = request.getInputStream();
            StringBuffer stringBuffer = new StringBuffer();
            int len = 0;
            byte[] bytes = new byte[1024];
            while ((len = is.read(bytes)) != -1){
                stringBuffer.append(new String(bytes, 0, len));
            }

            Integer orderId = Integer.valueOf(stringBuffer.toString());
            List<OrderItemVo> orderItemVoList = orderDao.showOrderDetail(orderId);
            StringBuffer msg = new StringBuffer();
            for (OrderItemVo orderItemVo : orderItemVoList) {
                msg.append(orderItemVo.getProductId()).append("@")
                        .append(orderItemVo.getImage()).append("@")
                        .append(orderItemVo.getBaseInformation()).append("@")
                        .append(orderItemVo.getPrice()).append("@")
                        .append(orderItemVo.getQuantity()).append("@")
                        .append(orderItemVo.getSumPrice()).append("#");
            }
            response.getWriter().print(msg.toString());
        }
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}
